<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <meta
      name="viewport"
      content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no"
    />

    <title>仿制腾讯视频的轮播图</title>
    <link rel="stylesheet" href="index.css" />

    <link rel="icon" type="image/x-icon" href="../../../favicon.ico" />
</head>

  <body>
    <div class="container" id="app">
      <div class="banner" id="banner"></div>
      <div class="title-box" id="titleBox"></div>
    </div>

    <script>
      // 轮播数据
      const list = [
        {
          title: "正在直播NBA",
          sub_title: "快船vs森林狼",
          image: "../../images/tenents/1.jpg",
          bg_color: "#53839a",
        },
        {
          title: "特战荣耀·热播",
          sub_title: "杨洋展特种军魂",
          image: "../../images/tenents/2.jpg",
          bg_color: "#bbbbaf",
        },
        {
          title: "新闻热点",
          sub_title: "本地新闻",
          image: "../../images/tenents/3.jpg",
          bg_color: "#d94e5d",
        },
        {
          title: "新闻热点",
          sub_title: "本地新闻",
          image: "../../images/tenents/4.jpg",
          bg_color: "#53839a",
        },
        {
          title: "新闻热点",
          sub_title: "本地新闻",
          image: "../../images/tenents/5.jpg",
          bg_color: "#bbbbaf",
        },
        {
          title: "新闻热点",
          sub_title: "本地新闻",
          image: "../../images/tenents/6.jpg",
          bg_color: "#d94e5d",
        }
      ];

      // 初始化状态
      let activeIndex = 0;
      let timer = null;
      const size = list.length;
      
      // 获取DOM元素
      const banner = document.getElementById('banner');
      const titleBox = document.getElementById('titleBox');
      const container = document.getElementById('app');
      
      // 生成标题
      function createTitles() {
        list.forEach((item, index) => {
          const titleDiv = document.createElement('div');
          titleDiv.className = `title ${index === activeIndex ? 'active' : ''}`;
          titleDiv.innerHTML = `${item.title}<span>${item.sub_title}</span>`;
          titleDiv.addEventListener('mouseover', () => changeBanner(index));
          titleBox.appendChild(titleDiv);
        });
      }
      
      // 停止自动播放
      function stopAutoPlay() {
        if (timer) {
          clearInterval(timer);
        }
      }
      
      // 开始自动播放
      function startAutoPlay() {
        timer = setInterval(() => {
          activeIndex = (activeIndex + 1) % size;
          changeBanner(activeIndex);
        }, 3000);
      }
      
      // 切换轮播
      function changeBanner(index) {
        stopAutoPlay();
        activeIndex = index;
        
        // 更新背景
        banner.style.backgroundImage = `url(${list[index].image})`;
        
        // 更新标题样式
        const titles = document.querySelectorAll('.title');
        titles.forEach((title, i) => {
          title.classList.toggle('active', i === index);
        });
        
        // 更新容器背景色
        container.style.backgroundColor = list[index].bg_color;
        
        startAutoPlay();
      }

      // 初始化
      document.addEventListener('DOMContentLoaded', () => {
        // 初始化创建标题
        createTitles();
        // 设置初始背景
        banner.style.backgroundImage = `url(${list[activeIndex].image})`;
        container.style.backgroundColor = list[activeIndex].bg_color;
        // 开始自动播放
        startAutoPlay();
      });
    </script>
  </body>
</html>
